package com.xh.controll;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xh.entiy.User;
import com.xh.service.UserService;

@Controller
public class UserControll {
	@Autowired
	private UserService userservice;
	@RequestMapping(value="/signin")
	@ResponseBody
	public void signin(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String email=request.getParameter("e-mail");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		String phonenumber=request.getParameter("phonenumber");
		User user=new User();
		user.setEmail(email);
		user.setName(username);
		user.setPassword(password);
		user.setPhone(phonenumber);
		System.out.println("emial是"+email+"---"+"username是"+username+"---"+"password是"+password+"---"+"phonenumber是"+phonenumber);
		userservice.SaveOrUpdateUser(user);
		response.sendRedirect("Login.html");
		
	}
	
	@RequestMapping(value="/admin/getUserinformation")
	@ResponseBody
	public List<User> getUserinformation() 
	{
		List<User> user=userservice.getUsers();
		return user;
		
	}
	
	@RequestMapping(value="/admin/edituser",method=RequestMethod.POST)
	@ResponseBody
	public void edituser(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException
	{
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String id=request.getParameter("edit-user-id");
		String email=request.getParameter("edit-user-email");
		String username=request.getParameter("edit-user-username");
		String password=request.getParameter("edit-user-password");
		String phonenumber=request.getParameter("edit-user-phonenumber");
		System.out.println("emial是"+email+"---"+"username是"+username+"---"+"password是"+password+"---"+"phonenumber是"+phonenumber);
		User user=userservice.getUser(Integer.valueOf(id));
		user.setEmail(email);
		user.setName(username);
		user.setPassword(password);
		user.setPhone(phonenumber);
		userservice.SaveOrUpdateUser(user);

	}
	@RequestMapping(value="/admin/deleteuser",method=RequestMethod.POST)
	@ResponseBody
	public void deleteuser(@RequestParam("id") Integer id)
	{
			User user=userservice.getUser(id);
			userservice.deleteUser(user.getId());
	}
	
	@RequestMapping(value="/admin/addusermethod",method=RequestMethod.POST)
	@ResponseBody
	public void addusermethod(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException
	{	
		System.out.println("********");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String email=request.getParameter("add-user-email");
		String username=request.getParameter("add-user-username");
		String password=request.getParameter("add-user-password");
		String phonenumber=request.getParameter("add-user-phonenumber");
		User user=new User();
		user.setName(username);
		user.setPassword(password);
		user.setPhone(phonenumber);
		user.setEmail(email);
		userservice.SaveOrUpdateUser(user);
		
	}

}
